package com.healthcloud.service.RegionalDisease.impl;


import com.healthcloud.dao.RegionalDisease.BaseAreaCodeDao;
import com.healthcloud.entity.HlHospitalEntity;
import com.healthcloud.entity.RegionalDisease.BaseAreaCodeEntity;
import com.healthcloud.service.RegionalDisease.BaseAreaCodeService;
import com.healthcloud.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("baseAreaCodeService")
@Transactional
public class BaseAreaCodeServiceImpl implements BaseAreaCodeService {
	@Autowired
	private BaseAreaCodeDao baseAreaCodeDao;
	
	@Override
	public BaseAreaCodeEntity queryObject(Long id){
		return baseAreaCodeDao.queryObject(id);
	}
	
	@Override
	public List<BaseAreaCodeEntity> queryList(BaseAreaCodeEntity bean, PageParam pageParam){
		return baseAreaCodeDao.queryList(bean, pageParam);
	}
	
	@Override
	public int queryTotal(BaseAreaCodeEntity bean){
		return baseAreaCodeDao.queryTotal(bean);
	}
	
	@Override
	public void save(BaseAreaCodeEntity baseAreaCode){
		baseAreaCodeDao.save(baseAreaCode);
	}
	
	@Override
	public void update(BaseAreaCodeEntity baseAreaCode){
		baseAreaCodeDao.update(baseAreaCode);
	}
	
	@Override
	public void delete(Long id){
		baseAreaCodeDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		baseAreaCodeDao.deleteBatch(ids);
	}


	@Override
	public List<HlHospitalEntity> queryByLargeAreaCode(Long largeAreaCode) {

		List<BaseAreaCodeEntity> list = baseAreaCodeDao.queryByLargeAreaCode(largeAreaCode);

		StringBuffer sb = new StringBuffer("(");

		if(list.size() == 1){

			sb.append("b.code like concat( ");
			sb.append(list.get(0).getCode());
			sb.append(",'%')");

		}else {
			for (int i = 0 ; i < list.size()-1;i++) {

				BaseAreaCodeEntity baseAreaCodeEntity = list.get(i);
				sb.append("b.code like concat( ");
				sb.append(baseAreaCodeEntity.getCode());
				sb.append(",'%')");
				sb.append(" or ");
			}

			sb.append("b.code like concat( ");
			sb.append(list.get(list.size()-1).getCode());
			sb.append(",'%')");
		}

		sb.append(")");
		return baseAreaCodeDao.queryByAreaCode(sb.toString(), 3);

	}

	@Override
	public List<HlHospitalEntity> queryByAreaCode(Long areaCode) {


		StringBuffer sb = new StringBuffer("(");
		sb.append("b.code like concat( ");
		sb.append(areaCode);
		sb.append(",'%')");
		sb.append(")");

		return baseAreaCodeDao.queryByAreaCode(sb.toString(),3);
	}

	@Override
	public String splitCode(Long areaCode) {
		List<BaseAreaCodeEntity> list = baseAreaCodeDao.queryByLargeAreaCode(areaCode);

		StringBuffer sb = new StringBuffer("(");

		if (areaCode<10){

			for (int i = 0 ; i < list.size()-1;i++) {

				BaseAreaCodeEntity baseAreaCodeEntity = list.get(i);
				sb.append("h.areaCode LIKE concat( ");
				sb.append(baseAreaCodeEntity.getCode());
				sb.append(",'%')");
				sb.append(" or ");
			}

			sb.append("h.areaCode LIKE concat( ");
			sb.append(list.get(list.size()-1).getCode());
			sb.append(",'%')");



		} else {
			sb.append(" h.areaCode LIKE concat( ");
			sb.append(areaCode);
			sb.append(" ,'%') ");
		}

		sb.append(")");
		return sb.toString();
	}
}
